Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document all the available Testcontainers integrations #44187

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

thecooldrop
Copy link

In this PR I have documented the ways in which containers provided by Testcontainers framework can be incorporated into the test tests managed by Spring's TestContext Framework.

In addition to describing how to use the containers, information on lifecycle of containers during tests has been added.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 9, 2025
@snicoll snicoll changed the title Add documentation on different ways of using test containers with Spring and information about lifecycle of containers (#43738) Document all the available Testcontainers integrations Feb 10, 2025
@snicoll snicoll added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 10, 2025
@snicoll snicoll added this to the 3.3.x milestone Feb 10, 2025
Copy link
Contributor

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clarification is great! I've left some comments that could help.

== Using via Spring managed beans

The containers provided by Testcontainers framework can be managed by Spring Boot as beans.
This method is often used in combination with javadoc:org.springframework.boot.testcontainers.service.connection.ServiceConnection[format=annotation].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ServiceConnection can be used with static fields and beans.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not quite understand what you mean with that, in context of the code which is selected here in comment. Is it that the sentence "This method is often used in combination with..." seems to point that it is the only combination in which @ServiceConnection annotation is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I rephrased the sentence so that it is clearer that this is not the only option. I hope now it is satisfactory. If not feel free to continue the discussion.


== Using via importing container declaration classes

A common pattern with Testcontainers framework is to declare the Container instances as static fields in an interface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it should highlight that this enables reusability of container configuration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that. I partially disagree though. As I am reading through rest of the Spring documentation I note that it is focused solely on documenting on which features are offered by Spring, and does not repeat or clarify software development "best practices". I followed this style here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as tip

== Lifecycle of managed containers

If you have used the annotations and extensions provided by Testcontainers framework, then the lifecycle of container instances is managed by the Testcontainers framework.
Please refer to the official documentation for the information about lifecycle of the containers, when managed by the Testcontainers framework.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Link to the official docs maybe

Copy link
Author

@thecooldrop thecooldrop Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Antora offer broken link checks? If so I will gladly add the link. I was cautious about inclusion of links, because links change and that can be irritating for the readers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as Antora attribute.


The test containers can be started multiple times.
Like any other beans the test containers are created and started once per application context managed by the TestContext Framework.
For details about how TestContext framework manages the underlying application contexts and beans therein, please refer to the official Spring documentation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add link?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that here you are refering to link to TestContext Framework docs.

Copy link
Author

@thecooldrop thecooldrop Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added reference to Spring docs, without deep linking to TestContext framework.

The container beans are stopped after the destruction of beans of other types.
This ensures that any beans depending on the functionalities provided by the containers are cleaned up first.

The containers are stopped as part of the application shutdown process, managed by the TestContext framework.
Copy link
Contributor

@eddumelendez eddumelendez Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is important to highlight. In some cases, for example, using activemq, kafka, pulsar and other containers, I recommend to declare it as a beans rather than static fields because that way the application context will shutdown the client first and then the container. Otherwise, when the container shutdown first the client will throw some errors because connection has been lost.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that you would like to have a concrete example in addition to the general explanation already provided, so that the readers can more clearly understand the "why" behind usage. Is my understanding correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as tips and notes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tips and notes are great! my examples were only to provide an explanation about the issue en general. 👍🏽

@thecooldrop
Copy link
Author

@eddumelendez Ready for re-review.

Copy link
Contributor

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left additional comments. LGTM, in general. Thanks again!

== Using via @Testcontainers JUnit5 extension

The Testcontainers provides JUnit5 extensions, which can be used to manage containers in your tests.
The extension is activated by applying the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incomplete

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wierd that I missed that, given that I know exactly what my thoughts were here. I will fill this up.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so I corrected this and completerd the sentence.

The container beans are stopped after the destruction of beans of other types.
This ensures that any beans depending on the functionalities provided by the containers are cleaned up first.

The containers are stopped as part of the application shutdown process, managed by the TestContext framework.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tips and notes are great! my examples were only to provide an explanation about the issue en general. 👍🏽

In this commit I have documented three methods of starting containers from
Testcontainer library as part of the Spring Boot tests.

Signed-off-by: Vanio Begic <[email protected]>
Concretely I have ensured that there are three lines between text and next header.
Also I added anchors to each header

Signed-off-by: Vanio Begic <[email protected]>
@thecooldrop
Copy link
Author

thecooldrop commented Feb 22, 2025

@eddumelendez I think we should be good to go now 🤞

Comment on lines +70 to +71
The test containers can be started multiple times.
Like any other beans the test containers are created and started once per application context managed by the TestContext Framework.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be just containers instead of test containers.

Copy link
Contributor

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants